// pages/news/list/index.js
var call = require("../../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageStatus: false,
    page: 1,
    limit: 10,
    total: 0,
    listData: [], // 消息数据
    dataStatus: false, // 是否已经到底
    keyName: '',
    chatUUID: '',
    typeData: [{
      name: '全部分类',
      id: '',
      index: 0
    }, {
      name: '物流信息',
      id: 1,
      index: 1
    }, {
      name: '付款信息',
      id: 2,
      index: 2
    }, {
      name: '退款信息',
      id: 3,
      index: 3
    }, {
      name: '充值信息',
      id: 4,
      index: 4
    }, {
      name: '月消费账单',
      id: 5,
      index: 5
    }, {
      name: '公告',
      id: 6,
      index: 6
    }, {
      name: '密码修改',
      id: 7,
      index: 7
    }, {
      name: '账号安全',
      id: 8,
      index: 8
    }, {
      name: '用户下单',
      id: 9,
      index: 9
    }, {
      name: '订单已完成',
      id: 10,
      index: 10
    }],
    typeIndex: 0, //  订单类型索引
    typeTitle: '全部分类',
    typeId: '',
    chatUUID: '',
    isSearch: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let {
      chatUUID,
      showName,
      isSearch
    } = options
    this.setData({
      chatUUID
    })
    wx.setNavigationBarTitle({
      title: showName
    })
    if (isSearch) {
      let {
        keyName
      } = options
      this.setData({
        isSearch,
        keyName
      })
      this.getSearchList(chatUUID, this.data.typeId)
    } else {
      this.getList(chatUUID, this.data.typeId)
    }
  },
  clear() {
    this.getSearchList(this.data.chatUUID, this.data.typeId)
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  //根据uuid 获取公告列表
  async getList(chatUUID, templateId, status) {
    wx.cloud.init()
    const {
      data: {
        code,
        errorMsg,
        data
      }
    } = await wx.cloud.callContainer({
      "config": {
        "env": "prod-0g38oh8hf0e4d5ac"
      },
      "path": "/v1/chat/" + chatUUID + '/' + this.data.page + '?templateId=' + templateId,
      "header": {
        "X-WX-SERVICE": "springboot-069k"
      },
      "method": "get",
      "data": {
        "action": "inc"
      }
    })
    console.log(data, code, errorMsg);
    if (code == 1) {
      let list = data.data.chatList
      this.setData({
        total: data.total
      })
      if (status) { // 触发搜索
        let listData = this.data.listData // 数据
        listData = listData.concat(list)
        this.setData({
          listData
        })
      } else {
        this.setData({
          listData: list
        })
      }
      if (list.length < this.data.limit && this.data.page != 1) {
        this.setData({
          dataStatus: true
        })
      }

    } else {
      wx.showToast({
        icon: 'none',
        title: errorMsg
      })
    }
    this.setData({
      pageStatus: true
    });
  },
  search() {
    if (this.data.keyName == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入搜索内容'
      })
      return false
    }
    this.getSearchList(this.data.chatUUID, this.data.typeId)
  },
  // 根据 uuid 和 关键字 搜索 获取公告列表
  async getSearchList(chatUUID, templateId) {
    wx.cloud.init()
    const {
      data: {
        code,
        errorMsg,
        data
      }
    } = await wx.cloud.callContainer({
      "config": {
        "env": "prod-0g38oh8hf0e4d5ac"
      },
      "path": "/v1/chat/search/" + chatUUID + '?templateId=' + templateId + '&searchWords=' + encodeURIComponent(this.data.keyName),
      "header": {
        "X-WX-SERVICE": "springboot-069k"
      },
      "method": "get",
      "data": {
        "action": "inc"
      }
    })
    console.log(data, code, errorMsg);
    if (code == 1) {
      let list = data.chatList
      this.setData({
        total: data.total
      })
      this.setData({
        listData: list
      })
      // if (status) { // 触发搜索
      //   let listData = this.data.listData // 数据
      //   listData = listData.concat(list)
      //   this.setData({
      //     listData
      //   })
      // } else {
      //   this.setData({
      //     listData: list
      //   })
      // }
      // if (list.length < this.data.limit && this.data.page != 1) {
      //   this.setData({
      //     dataStatus: true
      //   })
      // }

    } else {
      wx.showToast({
        icon: 'none',
        title: errorMsg
      })
    }
    this.setData({
      pageStatus: true
    });
  },
  onShow() {
    console.log(111)

  },

  // 订单类型改变
  goTypeChange(e) {
    console.log(e.currentTarget.dataset.item)
    let {
      id,
      index,
      name
    } = e.currentTarget.dataset.item
    this.setData({
      typeIndex: index,
      typeTitle: name,
      typeId: id,
      page: 1,
      dataStatus: false
    })
    if (this.data.isSearch) {
      this.getSearchList(this.data.chatUUID, id)
    } else {
      this.getList(this.data.chatUUID, id)
    }
    wx.pageScrollTo({
      scrollTop: 0
    })
    this.selectComponent('#itemType').toggle();
  },
  /**
   * 页面上拉触底事件的处理函数，与点击加载更多做同样的操作
   */
  onReachBottom() {
    if (this.data.isSearch) {
      return false
    }
    let page = this.data.page; // 页码
    let dataStatus = this.data.dataStatus
    if (!dataStatus) {
      this.setData({
        page: page + 1
      })
      this.getList(this.data.chatUUID, this.data.typeId, true) // 获取列表数据

      wx.showToast({
        title: '加载中...',
        icon: 'loading',
      })

    }
  },
  // 去列表
  async goDetails(e) {
    console.log('跳转');
    let {
      isLocal,
      isExists,
      wxAppId,
      targetHref
    } = e.currentTarget.dataset.item
    // console.log(isLocal);
    // wx.navigateTo({
    //   url: '/'+localTargetHref
    // })
    // return false
    if (isLocal == 1) {
      if (targetHref) {
        wx.navigateTo({
          url: targetHref
        })
      }
    } else {
      // console.log(targetHref);
      if (targetHref) {
        // 跳转小程序
        wx.navigateToMiniProgram({
          appId: wxAppId,
          path: targetHref,
          // path: '/' + 'subPackages/pages/standard/my/bill/blank',
          extraData: {
            foo: 'bar'
          },
          envVersion: 'release', // develop	开发版 trial	体验版 release	正式版
          success(res) {
            // 打开成功
            console.log('跳转成功')
          }
        })
        // wx.navigateTo({
        //   url: '/pages/news/web/index?path='+targetHref
        //   // url: 'https://helper.nuomayun.com/u/q'
        // })
        // var c = new cloud.Cloud({
        //   // 必填，表示是未登录模式
        //   identityless: true,
        //   // 资源方 AppID
        //   resourceAppid: 'wxedc030d07a67125a', // <!-- replace -->
        //   // 资源方环境 ID
        //   resourceEnv: 'prod-4g8cimqhe46b9a3c', // <!-- replace -->
        // })
        // await c.init()
        // wx.cloud.callContainer({
        //   config: {
        //     env: 'prod-4g8cimqhe46b9a3c',
        //   },
        //   path: '/v1/jump/info/' + '6fafb8fb-b1a3-4067-8627-975e3a54168f',
        //   method: 'GET',
        //   header: {
        //     'X-WX-SERVICE': 'springboot-l1ww',
        //   }
        // }).then(res => {
        //   console.log(res.data);

        // })
      }
    }
    return false
    switch (orderType) {
      case 1: // 预约
        // 预约详情
        wx.navigateTo({
          url: '/subPackages/pages/standard/my/order/index/details/index?id=' + order_id
        })
        break;
      case 2: // 商品
        // 商品详情
        wx.navigateTo({
          url: '/subPackages/pages/standard/my/order/shopping/details/index?id=' + order_id
        })
        break;
      case 3: // 自定义
      case 4: // 专家余额兑换商家余额
        break;
      case 5: // 团购
        // 团购详情
        wx.navigateTo({
          url: '/subPackages/pages/standard/my/order/groupPurchase/details/index?id=' + order_id
        })
        break;
    }
  },
})